package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import classes.Service;

/**
 *
 * @author Dan
 */
public class ServiceDAO {

    public static List<Service> getServices()
    {
        Connection con = getConnection();
        ArrayList<Service> services = new ArrayList<Service>();
        try {
            CallableStatement cs = con.prepareCall("{call stp_selAllServices(?)}");
            cs.setInt(1, 8); //hard coded now, needs to be current semester
            ResultSet rs = cs.executeQuery();

            while (rs.next())
            {
                int id = rs.getInt("activityid");
                String name = rs.getString("activity-name");
                Time startTime = rs.getTime("begin_time");
                Time endTime = rs.getTime("end_time");
                int driveTime = rs.getInt("travel_time");
                String location = rs.getString("location");
                boolean sun = rs.getBoolean("sun");
                boolean mon = rs.getBoolean("mon");
                boolean tue = rs.getBoolean("tue");
                boolean wed = rs.getBoolean("wed");
                boolean thurs = rs.getBoolean("thurs");
                boolean fri = rs.getBoolean("fri");
                boolean sat = rs.getBoolean("sat");
                Date startDate = rs.getDate("begin_date");
                Date endDate = rs.getDate("end_date");
                int transnumb = rs.getInt("transnumb");
                services.add(new Service(id, name, startTime, endTime, driveTime, location,
                        sun, mon, tue, wed, thurs, fri, sat, startDate, endDate, transnumb));
            }

            cs.close();
            con.close();
        }
        catch (SQLException e) {System.err.println(e.getMessage());e.printStackTrace();}
        return services;
    }
    
    public static List<Service> getServicesByDay(String day){
    	
        Connection con = getConnection();
        ArrayList<Service> services = new ArrayList<Service>();
        
        CallableStatement cs;
		try {
        	Statement stmt = con.createStatement();
        	String select = "SELECT * FROM dbo.COMSERV_ACTIVITY WHERE "+day+"=1 and travel_time > 0 and maxstu > 0 and semester_yearid = 10;";
        	ResultSet rs = stmt.executeQuery(select);;
	        
            while (rs.next())
            {
                int id = rs.getInt("activityid");
                String name = rs.getString("activity-name");
                Time startTime = rs.getTime("begin_time");
                Time endTime = rs.getTime("end_time");
                int driveTime = rs.getInt("travel_time");
                String location = rs.getString("address");
                boolean sun = rs.getBoolean("sun");
                boolean mon = rs.getBoolean("mon");
                boolean tue = rs.getBoolean("tue");
                boolean wed = rs.getBoolean("wed");
                boolean thurs = rs.getBoolean("thurs");
                boolean fri = rs.getBoolean("fri");
                boolean sat = rs.getBoolean("sat");
                Date startDate = rs.getDate("begin_date");
                Date endDate = rs.getDate("end_date");
                int transnumb = rs.getInt("transnumb");
                services.add(new Service(id, name, startTime, endTime, driveTime, location,
                        sun, mon, tue, wed, thurs, fri, sat, startDate, endDate, transnumb));
            }

            //cs.close();
            con.close();
	        
	        
		} catch (SQLException e) {
			System.err.println(e.getMessage());e.printStackTrace();
		}
		return services;
    }

   
    public static Service getServiceByID(int serviceID) {
        Connection con = getConnection();
        Service returnService = null;


        try {
        	Statement stmt = con.createStatement();
        	String select = "SELECT * FROM dbo.COMSERV_ACTIVITY WHERE activityid="+serviceID+";";
        	ResultSet rs = stmt.executeQuery(select);;

            while (rs.next())
            {
                int id = rs.getInt("activityid");
                String name = rs.getString("activity-name");
                Time startTime = rs.getTime("begin_time");
                Time endTime = rs.getTime("end_time");
                int driveTime = rs.getInt("travel_time");
                String location = rs.getString("address");
                boolean sun = rs.getBoolean("sun");
                boolean mon = rs.getBoolean("mon");
                boolean tue = rs.getBoolean("tue");
                boolean wed = rs.getBoolean("wed");
                boolean thurs = rs.getBoolean("thurs");
                boolean fri = rs.getBoolean("fri");
                boolean sat = rs.getBoolean("sat");
                Date startDate = rs.getDate("begin_date");
                Date endDate = rs.getDate("end_date");
                int transnumb = rs.getInt("transnumb");
                returnService = new Service(id, name, startTime, endTime, driveTime, location,
                        sun, mon, tue, wed, thurs, fri, sat, startDate, endDate, transnumb);
            }

            con.close();
        }
        catch (SQLException e) {e.printStackTrace();}
        return returnService;
    }
    
    public static Connection getConnection()
    {
        DBConn db = new DBConn("BUNSEN");
        return db.getConnection();
    }
}